package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cqut.dto.LimitShowDTO;
import com.cqut.service.IEvaluationResultManageService;
import com.cqut.util.ResponseUtil;

@RestController
@RequestMapping("api/evaluationResultManage")
public class EvaluationResultManageController {
	@Resource(name="evaluationResultManageService")
	private IEvaluationResultManageService evaluationResultManageService;
	
	/**
	 * 初始化列表
	 * @param limitShowDTO
	 * @param errors
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public ResponseEntity<String> list(
			@Validated
			LimitShowDTO limitShowDTO ,BindingResult errors){
		
		if(errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
		
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String,Object>> signUpList = evaluationResultManageService.getEvaluationResultManageList();
		signUpList = getProcess(signUpList);
		
		long count = evaluationResultManageService.count();
		result.put("rows",signUpList);
		result.put("total", count);
		System.out.println("数据："+signUpList);
		
		return ResponseUtil.getResEntityForGetAndJson(result);
	}
	
	/**
	 * 得到下拉框阶段名称内容
	 */
	@RequestMapping(value="/state2",method=RequestMethod.GET)
	public ResponseEntity<String> getStateName(){
		Map<String,Object> result = new HashMap<String,Object>();
		List<Map<String,Object>> states = evaluationResultManageService.getStateName();
		
		result.put("rows", states);
		System.out.println("阶段名称"+states);
		return ResponseUtil.getResEntityForGetAndJson(result);
	}
	
	/**
	 * 得到下拉框赛制流程内容
	 */
	@RequestMapping(value="/formatProcessList",method=RequestMethod.GET)
	public ResponseEntity<String> getFormatProcess(){
		Map<String,Object> result = new HashMap<String,Object>();
		List<Map<String,Object>> list = evaluationResultManageService.getFormatProcess();
		result.put("rows",list);
		System.out.println("赛制流程"+list);
		return ResponseUtil.getResEntityForGetAndJson(result);
		
	}
	
	/**
	 * 得到下拉框评审状态内容
	 */
	@RequestMapping(value="/reviewState",method=RequestMethod.GET)
	public ResponseEntity<String> getReviewState(){
		Map<String,Object> result= new HashMap<String,Object>();
		List<Map<String,Object>> list = evaluationResultManageService.getReviewState();
		result.put("rows",list);
		System.out.println("评审状态"+list);
		return ResponseUtil.getResEntityForGetAndJson(result);
	}
	
	
	
	/**
	 * 得到详情
	 */
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	public ResponseEntity<String> getDetail(
			@RequestParam("signUpId")
			String signUpId){
		System.out.println("需要查看的id"+signUpId);
		
		if(signUpId == null)
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();
        Map<String, Object> result = evaluationResultManageService.getDetail(signUpId);
        System.out.println("详细："+result);
		return ResponseUtil.getResEntityForGetAndJson(result);
	}
	
	/**
	 * 解析isPromoted即赛制流程
	 */
	public List<Map<String,Object>> getProcess(List<Map<String,Object>> signUpList){
		for(int i = 0;i<signUpList.size();i++){
			if(signUpList.get(i).get("isPromoted").equals(0)){
				signUpList.get(i).put("isPromoted", "淘汰");
			}else if(signUpList.get(i).get("isPromoted").equals(1)){
				signUpList.get(i).put("isPromoted", "初赛");
			}else if(signUpList.get(i).get("isPromoted").equals(2)){
				signUpList.get(i).put("isPromoted", "决赛");
			}else if(signUpList.get(i).get("isPromoted").equals(3)){
				signUpList.get(i).put("isPromoted", "复活赛");
			}
		}
		return signUpList;
	}
}
